﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Dictionary.ERP;

namespace Dictionary
{
    public class xucListState : UserControl
    {
        public delegate void ClickedEventHander(object sender, string Value);

        private ButtonEdit btSearch;
        private IContainer components;
        private DataTable dt_State;
        private ImageCollection imgList;
        private ImageListBoxControl imgListState;
        private SelectionMode m_ListSelectionMode = SelectionMode.One;
        private PanelControl panelControl1;

        public xucListState()
        {
            InitializeComponent();
        }

        public event ClickedEventHander Clicked;

        private void btSearch_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            Search(dt_State, btSearch.Text);
        }

        private void btSearch_EditValueChanged(object sender, EventArgs e)
        {
            Search(dt_State, btSearch.Text);
            if (btSearch.Text == "")
            {
                btSearch.EditValue = null;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private string GetCode(ImageListBoxItem item)
        {
            try
            {
                string str = item.Value.ToString();
                char[] separator = {'(', ')'};
                return str.Split(separator)[1];
            }
            catch
            {
                return "";
            }
        }

        private void imgListEmployee_KeyDown(object sender, KeyEventArgs e)
        {
            imgListState.SelectionMode = SelectionMode.MultiSimple;
            if ((e.Modifiers == Keys.Control) && (e.KeyCode == Keys.A))
            {
                imgListState.SelectAll();
            }
        }

        private void imgListState_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                RaiseClickedEventHander(GetCode(imgListState.SelectedItem as ImageListBoxItem));
            }
            catch
            {
            }
            if (m_ListSelectionMode == SelectionMode.One)
            {
                if (ModifierKeys != Keys.None)
                {
                    imgListState.SelectionMode = SelectionMode.MultiSimple;
                }
                else
                {
                    imgListState.SelectionMode = SelectionMode.One;
                }
            }
        }

        private void InitializeComponent()
        {
            components = new Container();
            var manager = new ComponentResourceManager(typeof (xucListState));
            var appearance = new SerializableAppearanceObject();
            imgListState = new ImageListBoxControl();
            imgList = new ImageCollection(components);
            panelControl1 = new PanelControl();
            btSearch = new ButtonEdit();
            ((ISupportInitialize) imgListState).BeginInit();
            imgList.BeginInit();
            panelControl1.BeginInit();
            panelControl1.SuspendLayout();
            btSearch.Properties.BeginInit();
            base.SuspendLayout();
            imgListState.Dock = DockStyle.Fill;
            imgListState.HighlightedItemStyle = HighlightStyle.Skinned;
            imgListState.HotTrackItems = true;
            imgListState.HotTrackSelectMode = HotTrackSelectMode.SelectItemOnClick;
            imgListState.ImageList = imgList;
            imgListState.Location = new Point(0, 0x2e);
            imgListState.Name = "imgListState";
            imgListState.Size = new Size(0x115, 430);
            imgListState.TabIndex = 0;
            imgListState.SelectedIndexChanged += imgListState_SelectedIndexChanged;
            imgListState.KeyDown += imgListEmployee_KeyDown;
            imgList.ImageStream = (ImageCollectionStreamer) manager.GetObject("imgList.ImageStream");
            imgList.Images.SetKeyName(0, "folder.png");
            panelControl1.Controls.Add(btSearch);
            panelControl1.Dock = DockStyle.Top;
            panelControl1.Location = new Point(0, 0);
            panelControl1.Name = "panelControl1";
            panelControl1.Size = new Size(0x115, 0x2e);
            panelControl1.TabIndex = 1;
            btSearch.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            btSearch.EditValue = @"[Lọc công đoạn theo từ khóa]";
            btSearch.Location = new Point(5, 12);
            btSearch.Name = "btSearch";
            btSearch.Properties.Appearance.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            btSearch.Properties.Appearance.Options.UseFont = true;
            btSearch.Properties.Buttons.AddRange(new[]
            {
                new EditorButton(ButtonPredefines.Glyph, "T\x00ecm", -1, true, true, false, ImageLocation.MiddleCenter,
                    (Image) manager.GetObject("btSearch.Properties.Buttons"), new KeyShortcut(Keys.None), appearance, "",
                    null, null, true)
            });
            btSearch.Properties.NullText = @"[Lọc nhân viên theo từ khóa]";
            btSearch.Size = new Size(0x10b, 0x16);
            btSearch.TabIndex = 0;
            btSearch.ButtonClick += btSearch_ButtonClick;
            btSearch.EditValueChanged += btSearch_EditValueChanged;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(imgListState);
            base.Controls.Add(panelControl1);
            base.Name = "xucListState";
            base.Size = new Size(0x115, 0x1dc);
            ((ISupportInitialize) imgListState).EndInit();
            imgList.EndInit();
            panelControl1.EndInit();
            panelControl1.ResumeLayout(false);
            btSearch.Properties.EndInit();
            base.ResumeLayout(false);
        }

        public void ListSelectionMode(SelectionMode mode)
        {
            imgListState.SelectionMode = m_ListSelectionMode = mode;
        }

        public void LoadData()
        {
            dt_State = new DIC_STATE().GetList();
            imgListState.Items.Clear();
            foreach (DataRow row in dt_State.Rows)
            {
                var item = new ImageListBoxItem
                {
                    ImageIndex = 0,
                    Value = "(" + row["StateCode"] + ") - " + row["StateName"]
                };
                imgListState.Items.Add(item);
            }
        }

        public void RaiseClickedEventHander(string Value)
        {
            if (Clicked != null)
            {
                Clicked(this, Value);
            }
        }

        public void Search(DataTable States, string Value)
        {
            imgListState.Items.Clear();
            foreach (DataRow row in States.Rows)
            {
                if (row["StateCode"].ToString().ToLower().Contains(Value.ToLower()) ||
                    row["StateName"].ToString().ToLower().Contains(Value.ToLower()))
                {
                    var item = new ImageListBoxItem
                    {
                        ImageIndex = 0,
                        Value = "(" + row["StateCode"] + ") - " + row["StateName"]
                    };
                    imgListState.Items.Add(item);
                }
            }
        }
    }
}